﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "ResourceOperation/AsynchronousLoading.h"
#include "Engine/AssetManager.h"

void UAsynchronousLoading::SingleRequestAsyncLoadMethod()
{
	FStreamableManager& streamableManager = UAssetManager::GetStreamableManager();
	FStreamableDelegate streamableDelegate = FStreamableDelegate::CreateUObject(this, &UAsynchronousLoading::OnSingleAssetLoadFinshed);
	streamableManager.RequestAsyncLoad(SingeleObject, streamableDelegate);
}

void UAsynchronousLoading::MultiRequestAsyncLoadMethodOne()
{
	FStreamableManager& streamableManager = UAssetManager::GetStreamableManager();
	FStreamableDelegate streamableDelegate = FStreamableDelegate::CreateUObject(this, &UAsynchronousLoading::OnMultiAssetsLoadFinshedOne);
	streamableManager.RequestAsyncLoad(ObjectList1, streamableDelegate);
}

void UAsynchronousLoading::MultiRequestAsyncLoadMethodTwo()
{
	FStreamableManager& streamableManager = UAssetManager::GetStreamableManager();
	FStreamableDelegate streamableDelegate;
	streamableDelegate.BindUObject(this, &UAsynchronousLoading::OnMultiAssetsLoadFinshedTwo);

	TArray<FSoftObjectPath> SoftPathList;
	for (int32 i = 0; i < ObjectList2.Num(); i++)
	{
		SoftPathList.Add(ObjectList2[i].ToSoftObjectPath());
	}
	streamableManager.RequestAsyncLoad(SoftPathList, streamableDelegate);
}

void UAsynchronousLoading::SingleBPClassLoadMethod()
{
	FStreamableManager& streamableManager = UAssetManager::GetStreamableManager();
	FStreamableDelegate streamableDelegate = FStreamableDelegate::CreateUObject(this, &UAsynchronousLoading::OnSingleClassLoadFinshed);
	streamableManager.RequestAsyncLoad(SingleClassPath, streamableDelegate);
}

void UAsynchronousLoading::OnSingleAssetLoadFinshed()
{
	FSoftObjectPtr SoftObjPtr = FSoftObjectPtr(SingeleObject);
	UStaticMesh* mesh = Cast<UStaticMesh>(SoftObjPtr.Get());
	if (mesh)
	{
		// UE_LOG(LogTemp, Warning, TEXT("mesh name:%s"), *mesh->GetName());
	}
}

void UAsynchronousLoading::OnMultiAssetsLoadFinshedOne()
{
	for (auto AssetItem : ObjectList1)
	{
		FSoftObjectPtr SoftObjPtr = FSoftObjectPtr(AssetItem); //此处也可用 TSoftObjectPtr<T>
		UStaticMesh* mesh = Cast<UStaticMesh>(SoftObjPtr.Get());
		if (mesh)
		{
			// UE_LOG(LogTemp, Warning, TEXT("mesh name:%s"), *mesh->GetName());
		}
	}
}

void UAsynchronousLoading::OnMultiAssetsLoadFinshedTwo()
{
	for (auto AssetItem : ObjectList2)
	{
		UTexture2D* ItemTex = AssetItem.Get();
		if (ItemTex)
		{
			// UE_LOG(LogTemp, Warning, TEXT("Texture2D name:%s"), *ItemTex->GetName());
		}
	}
}

void UAsynchronousLoading::OnSingleClassLoadFinshed()
{
	TSoftClassPtr<AActor> ItemPtr = TSoftClassPtr<AActor>(SingleClassPath);
	UClass* ItemClass = ItemPtr.Get();
	if (ItemClass)
	{
		// UE_LOG(LogTemp, Warning, TEXT("Actor name:%s"), *ItemClass->GetName());
	}
}
